<template>
	<view>
		<view class="head">
			<image :src="groupInfo.background" mode="heightFix"></image>
		</view>
		<!--基本信息-->
		<view class="groupInfo">
			<view class="info">
				<view class="avatar">
					<image :src="groupInfo.avatar" mode="aspectFill"></image>
				</view>
				<view class="detail">
					<view class="title">
						<view class="title_name">
							{{groupInfo.name}}
						</view>
						<image
							src="/static/icons/QRcode.png" 
							@click="openQRcode"
							class="title_after"
							style="width: 30rpx;height: 30rpx;"
						></image>
						<image v-if="groupInfo.isCreateUser" src="../../static/icons/delete.png" class="deleteIcon" ></image>
					</view>
					<view class="leader">团长：{{groupInfo.createUser}}</view>
					<view class="tags">
						<view class="tag">
							{{groupInfo.acgnName}}
						</view>	
					</view>
				</view>
			</view>
			<!--团员看到的通知-->
			<view class="notice">
				<view class="header">
					<image src="../../static/icons/attention.png"></image>
					<view>开团公告</view>
					<image v-if="groupInfo.isCreateUser" src="../../static/icons/edit.png" class="editIcon" @click="updateNotice"></image>
				</view>
				<view class="content">{{groupInfo.notice}}</view>
			</view>
			<!-- 团员管理 -->
			<!-- <view class="management">
				<view class="member" style="font-size: 30rpx;">
					团员管理
				</view>
				<view class="arrow" @click="goToMember">
					<image src="../../static/icons/right.png" mode=""></image>
				</view>
			</view> -->
			<!--开谷列表-->
			<view class="openingGoods">
				<view style="margin: 0 30rpx;">开谷列表</view>
				<view class="tabbar">
					<view class="tabBarItem" :class="current==index?'active':''" v-for="(item,index) in tabBarTitle" :key="index" @click="changeScene(index)">
						{{item}}
					</view>
				</view>
				<view class="goods">
					<view class="goodsList" v-for="(item,index) in showList" @click="gotoGoodsDetail(item.id)">
						<goodsCard>
							<template v-slot:coverImg>
								<image :src="item.cover" mode="widthFix" class="coverImg"></image>
							</template>
							<template v-slot:goodsName>
								{{item.name}}
							</template>
							<template v-slot:tag>
								<view v-for="(tag, index) in item.tags" :key="index" class="tag">
								    <text>{{ tag }}</text>
								</view>
							</template>
							<template v-slot:price>
								<text style="font-size: 40rpx;color: red;">{{item.averagePrice}}</text>
							</template>
						</goodsCard>
					</view>
				</view>
			</view> 
			<!--加团按钮 3种状态-->
			<view v-if="!groupInfo.isCreateUser">
				<uni-fab 
					:pattern="pattern" 
					:content="content" 
					horizontal="right" 
					vertical="bottom"
					direction="vertical" 
					@trigger="trigger"/>
			</view>
			<!-- 修改团公告 -->
			<view v-if="isPopupAShow" class="popupWrap">
				<view class="popup-content">
					<view class="notice">
						<view class="title">
							<view class="">
								更改团公告
							</view>
							<view class="back" @click="hiddenPop">
								返回
							</view>
						</view>
						<view class="noteContent">
							<textarea name="" id="" cols="30" rows="10" v-model="newNotice" style="width: 90%;"></textarea>
						</view>
						<view class="confirmBtn" @click="popAConfirm">
							确定
						</view>
					</view>
				</view>
			</view>
			<!-- 开谷按钮 -->
			<view class="createGoods" @click="gotoGoods" v-if="groupInfo.isCreateUser">
				开谷
			</view>
			<!-- 展示群聊二维码 -->
			<view v-if="isPopupBShow" class="popupWrap">
				<view class="popup-content">
					<view class="back" @click="hiddenPop" style="color:#8DC7DF;text-align: right;margin-right: 45rpx;">
						返回
					</view>
					<view class="qrcode">
						<image :src="groupInfo.qrCode" mode="widthFix"></image>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import goodsCard from "../../components/goodsCard/goodsCard.vue";
	import {getGroupDetail,updateGroupNotice,joinGroup,removeJoin} from '@/api/group.js'
	import {addFavor,removeFavor} from '@/api/favorite.js'
	export default {
		components:{
			goodsCard
		},
	    onShow() {
			let pages=getCurrentPages()
			this.groupId=pages[pages.length-1].options.groupId
			this.getGroupDetail()
		},
		onLoad() {
			
		},
		data() {
			return {
				groupId:null,
				groupInfo:{},
				current:0,
				tabBarTitle:['全部','正在开谷','待开谷'],
				showList:[],
				isPopupAShow: false, // 控制弹窗显示与隐藏
				isPopupBShow:false,
				newNotice:'',
				pattern: {
							color: '#8DC7DF',
							backgroundColor: '#fff',
							selectedColor: '#007AFF',
							buttonColor: '#8DC7DF',
							iconColor: '#fff'
				},
				content: [{
							iconPath: '/static/icons/collect.png',
							selectedIconPath: '/static/icons/collected.png',
							text: '关注',
							active: false
				}],
				
			};
		},
		methods:{
			getGroupDetail(){
				getGroupDetail(this.groupId).then(res=>{
					if(res.data.code==200){
						this.groupInfo=res.data.data
						this.showList=this.groupInfo.allGoodsList
						this.newNotice=this.groupInfo.notice
						if(this.groupInfo.isFavorGroup===true){
							this.content[0].active=true
							this.content[0].text='取消关注'
						}else if(this.groupInfo.isFavorGroup===false){
							this.content[0].active=false
							this.content[0].text='关注'
						}
					}
				})
			},
			goToMember(){
				uni.navigateTo({
					url:`/pages/groupDetails/members?groupId=${this.groupInfo.id}`
				})
			},
			gotoGoods(){
				uni.navigateTo({
					url:`/pages/createGoods/createGoods?groupId=${this.groupInfo.id}`
				})
			},
			gotoGoodsDetail(id){
				uni.navigateTo({
					url:`/pages/goodsDetails/goodsDetails?goodsId=${id}`
				})
			},
			changeScene(index){
				this.current=index
				if(index==0){
					this.showList=this.groupInfo.allGoodsList
				}
				if(index==1){
					this.showList=this.groupInfo.openingGoodsList
				}
				if(index==2){
					this.showList=this.groupInfo.waitOpenGoodsList
				}
			},
			updateNotice(){
				this.isPopupAShow=true
			},
			openQRcode(){
				this.isPopupBShow=true
			},
			popAConfirm(){
				let data={
					groupId:this.groupInfo.id,
					notice:this.newNotice
				}
				updateGroupNotice(data).then(res=>{
					if(res.data.code==200){
						this.isPopupAShow=false
						this.getGroupDetail()
					}
				})
			},
			hiddenPop(){
				this.isPopupAShow=false
				this.isPopupBShow=false
			},
			trigger(e){
				if(e.index==0){
				let that=this
				uni.showModal({
					title: '提示',
					content: `您${this.content[e.index].active ? '确定要取消关注团: ' : '确定要关注团: '}${this.groupInfo.name} ?`,
					success: function(res) {
						if (res.confirm) {
							if(e.item.active==false){
								addFavor(that.groupInfo.id).then(res=>{
									if(res.data.code==200){
										e.item.active=true
										e.item.text='取消关注'
									}
								})
							}else{
								let data={
									groupIdList:[that.groupInfo.id],
								}
								removeFavor(data).then(res=>{
									if(res.data.code==200){
										e.item.active=false
										e.item.text='关注'
									}
								})
							}
						} else if (res.cancel) {
							
						}
						}
					})
				}
			},
		}
	}
</script>

<style lang="scss">
.head{
	display: flex;
	justify-content: center;
	align-items: center;
	height: 500rpx;
	image{
		height: 100%;
	}
}
.groupInfo {
	margin-top: 10rpx;
	.info{
		display: flex;
		height: 150rpx;
		margin: 0 30rpx 30rpx 20rpx;
		padding-bottom: 20rpx;
		border-bottom: 1rpx solid #8DC7DF;
		.avatar{
			margin-left: 20rpx;
			flex: 1;
			image{
				width: 140rpx;
				height: 140rpx;
				border-radius: 50%;
				background-color: #ccc;
			}
		}
		.detail{
			flex: 3;
			display: flex;
			flex-direction: column;
			font-size: 30rpx;
			.title{
				display: flex;
				align-items: center;
				margin-left: 20rpx;
				position: relative;
				.title_name{
					font-size: 32rpx;
					margin: 0 10rpx;
				}
				.deleteIcon{
					position: absolute;
					right: 20rpx;
					width: 40rpx;
					height: 40rpx;
				}
			}
			.leader{
				font-size: 22rpx;
				margin: 14rpx 30rpx;
			}
			.tags{
				display: flex;
				.tag{
					height: 30rpx;
					line-height: 30rpx;
					font-size: 26rpx;
					color: #8DC7DF;
					border: 1rpx solid #8DC7DF;
					border-radius: 20rpx;
					margin: 0 10rpx;
					padding: 10rpx 20rpx;
				}
				
			}
		}
	}
	.notice{
		font-size: 34rpx;
		margin: 0 30rpx 30rpx 30rpx;
		padding-bottom: 20rpx;
		.header{
			display: flex;
			align-items: center;
			position: relative;
			image{
				width: 40rpx;
				height: 40rpx;
				margin-right: 12rpx;
			}
			.editIcon{
				position: absolute;
				width: 40rpx;
				height: 40rpx;
				right: 8rpx;
			}
		}
		.content{
			padding: 0 20rpx;
			font-size: 28rpx;
			line-height: 46rpx;
			color: #666666;
			margin-top: 12rpx;
		}
	}
	.management{
		font-size: 34rpx;
		padding-left: 30rpx;
		height: 100rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 20rpx;
		.arrow {
			position: absolute;
			right: 45rpx;
			image{
				width: 30rpx;
				height: 30rpx;
			}
		}
	}
	.openingGoods{
		font-size: 34rpx;
		padding-bottom: 120rpx;
		.tabbar{
			display: flex;
			height: 80rpx;
			line-height: 80rpx;
			background-color: #F3F9FC;
			color: #9A9A9A;
			margin: 20rpx 0;
			font-size: 30rpx;
			.tabBarItem{
				margin: 0 20rpx;
				&.active{
					color: #8DC7DF;
					font-size: 32rpx;
				}
			}
			
		}
		.goods{
			/* display: flex;
			flex-wrap: wrap;
			height: 100vh; */
			column-count: 2;
			column-gap: 20rpx;
			padding: 0 30rpx;
			/* ------------------------ */
			.groupInfo{
				height: 0;
			}
			.headImg{
				width: 50rpx;
				height: 50rpx;
				border-radius: 50%;
				margin-right: 20rpx;
			}
			.coverImg {
				width: 100%;
			}
			.tags .tag {
			    display: inline-block;
			    height: 40rpx;
			    padding: 0 14rpx;
			    margin: 10rpx 10rpx 10rpx 0;
			    line-height: 40rpx;
				color: #347CD4;
			    font-size: 20rpx;
			    border-radius: 20rpx;
				background-color: #E3F1F7;
			}
		}
	}
}

.popupWrap {
		width: 100vw;
		height: 100vh;
		background-color: rgba(0, 0, 0, .1);
		position: absolute;
		left: 50%;
		top: 50%;
		transform: translate(-50%, -50%);
		display: flex;
		justify-content: center;
		align-items: center;
		.popup-content {
				padding: 20px;
				width: 80%;
				background-color: white;
				// text-align: center;
				position: absolute;
				left: 50%;
				top: 50%;
				transform: translate(-50%, -50%);
				z-index: 999;
				/* 确保弹窗在内容上层显示 */
			.notice{
				display: flex;
				flex-direction: column;
				.title{
					display: flex;
					justify-content: space-between;
					height: 80rpx;
					.back{
						color:#8DC7DF;
					}
				}
				.noteContent {
					width: 90%;
				    /* margin: 14rpx; */
				    border: 1px solid #eee;
				    font-size: 30rpx;
				    padding: 16rpx;
				}
				.confirmBtn{
					width: 100%;
					height: 70rpx;
					line-height: 70rpx;
					text-align: center;
					color: #fff;
					font-size: 30rpx;
					background-color: #8DC7DF;
					border-radius: 30rpx;
					margin-top: 20rpx;
				}
			}
			.qrcode{
				width: 90%;
				image{
					width: 100%;
				}
			}
			
		}
}
.createGoods{
	position: fixed;
	width: 90%;
	height: 80rpx;
	line-height: 80rpx;
	border-radius: 40rpx;
	bottom: 20rpx;
	text-align: center;
	color:#fff;
	background-color: #8DC7DF;
	left: 50%;
	transform: translate(-50%,0);
	
}

.uni-fab__content--other-platform{
	box-shadow: none!important;
}
</style>
